<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="供应商名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model.trim="model.name" placeholder="请输入供应商名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="付款方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="paymentMethod">
              <j-search-select-tag placeholder="请选择付款方式" v-model="model.paymentMethod" dict="product_supplier_payment_method"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="经营类目" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="categoryId">
              <a-tree-select
                v-model="model.categoryId"
                :replaceFields="replaceFields"
                :filterTreeNode="filterTreeNode"
                show-search
                style="width: 100%"
                :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
                :tree-data="treeData"
                placeholder="请选择经营类目"
                tree-default-expand-all
                :getPopupContainer="node => node.parentNode"
              ></a-tree-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="采购员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="buyer">
              <j-search-select-user v-model="model.buyer" placeholder="请输入并选择采购员"></j-search-select-user>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="联系人姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contactName">
              <a-input v-model.trim="model.contactName" placeholder="请输入联系人姓名"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="联系电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contactPhone">
              <a-input v-model.trim="model.contactPhone" placeholder="请输入联系电话"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="90天退款率" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ninetyDayRefundRate">
              <a-input-number v-model="model.ninetyDayRefundRate" placeholder="请输入90天退款率" style="width: 50%" /> %
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="2年退款率" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="twoYearRefundRate">
              <a-input-number v-model="model.twoYearRefundRate" placeholder="请输入2年退款率" style="width: 50%" /> %
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="回货率" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="returnRate">
              <a-input-number v-model="model.returnRate" placeholder="请输入回货率" style="width: 50%" @blur="handleChange" /> %
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="supplierStatus">
              <j-dict-select-tag type="list" v-model="model.supplierStatus" dictCode="product_supplier_status" placeholder="请选择状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="是否滞留可退" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isRetentionRefund">
              <j-dict-select-tag  v-model="model.isRetentionRefund" placeholder="请选择是否滞留可退" type="radio"  dictCode="yn"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea placeholder="请输入备注" :rows="3" v-model="model.remark"/>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="12">
            <a-form-model-item label="类目" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="categoryId">
              <j-dict-select-tag type="list" v-model="model.categoryId" dictCode="" placeholder="请选择类目" />
            </a-form-model-item>
          </a-col> -->
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import { productMixins } from '@/views/productLibrary/mixins/productMixins'
  export default {
    name: 'ProductLibrarySupplierForm',
    mixins:[productMixins],
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          // categoryId: [
          //   { required: true, message: '请输入类目!'},
          // ],
          name: [
            { required: true, message: '请输入名称!'},
          ],
          paymentMethod: [
            { required: true, message: '请输入付款方式!'},
          ],
          buyer: [
            { required: true, message: '请输入采购员!'},
          ],
          categoryId: [
            { required: true, message: '请选择经营类目!'}
          ],
          contactPhone: [
            {validator:this.validateMobile}
          ]
          // supplierStatus: [
          //   { required: true, message: '请输入供应商状态!'},
          // ],
        },
        url: {
          add: "/hzkj-product/product/productLibrary/supplier/add",
          edit: "/hzkj-product/product/productLibrary/supplier/edit",
          queryById: "/hzkj-product/product/productLibrary/supplier/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      }
    },
    created () {
      //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
        this.loadData_('', 'queryMyCategory', this.model.categoryId);
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      validateMobile(rule,value,callback){                 
        if (!value || new RegExp(/^1([38][0-9]|4[579]|5[0-3,5-9]|6[25789]|7[0135678]|9[89])\d{8}$/).test(value)){
          callback();
        }else{
          callback("您的手机号码格式不正确");
        }
      },
      handleChange(){
        if(this.model.returnRate >= 0 && this.model.returnRate < 10){
          this.$set(this.model, 'supplierStatus', '1');
        }else if(this.model.returnRate >= 10 && this.model.returnRate < 40){
          this.$set(this.model, 'supplierStatus', '2');
        }else if(this.model.returnRate >= 40 && this.model.returnRate < 80){
          this.$set(this.model, 'supplierStatus', '3');
        }else if(this.model.returnRate >= 80 && this.model.returnRate < 100){
          this.$set(this.model, 'supplierStatus', '4');
        }else if(this.model.returnRate == 100){
          this.$set(this.model, 'supplierStatus', '5');
        }else{
          this.$set(this.model, 'supplierStatus', '');
        }
      }
    }
  }
</script>